package com.actionsky.entity;

public class SubPages {
    private int each_disNums;	// 每页显示记录数
    private int nums;	// 总记录数
    private int current_page;	// 当前页
    private int sub_pages;	// 每次显示页数
    private int pageNums;// 总页数 总数/每次显示页数
    // private java.util.ArrayList<Integer> page_array;// 用來構造分頁的數組
    private String subPage_link;// 每個分頁的鏈接
    // private int subPage_type;// 顯示分頁的類型
    /*
     * 當subPage_type=1的時候為普通分頁模式 example： 共4523條記錄,每頁顯示10條,當前第1/453頁 [首頁] [上頁]
     * [下頁] [尾頁] 當subPage_type=2的時候為經典分頁樣式 example： 當前第1/453頁 [首頁] [上頁] 1 2 3 4
     * 5 6 7 8 9 10 [下頁] [尾頁]
     */
    public SubPages(int each_disNums, int nums, int current_page,
            int sub_pages, String subPage_link) {
        this.each_disNums = each_disNums;
        this.nums = nums;
        if (current_page < 1) {
            this.current_page = 1;
        } else {
            this.current_page = current_page;
        }
        this.sub_pages = sub_pages;
        this.pageNums = (int) java.lang.Math.ceil(nums / each_disNums);
        this.subPage_link = subPage_link;
        // this.show_SubPages(subPage_type);
        // echo pageNums."--".sub_pages;
 
    }
 
    /*
     * show_SubPages函數用在構造函數里面。而且用來判斷顯示什麼樣子的分頁type
     */
    // private String show_SubPages(int subPage_type){
    // if(subPage_type == 1){
    // return this.subPageCss1();
    // }else if (subPage_type == 2){
    // return this.subPageCss2();
    // }
    // }
    /*
     * 用來給建立分頁的數組初始化的函數。1,2,3,4,5,6.....
     */
    public java.util.ArrayList<Integer> initArray() {
        java.util.ArrayList<Integer> page_array = new java.util.ArrayList<Integer>();
        for (int i = 0; i < this.sub_pages; i++) {
            page_array.add(i, i);
            ;
        }
        return page_array;
    }
 
    /*
     * construct_num_Page該函數使用來構造顯示的條目 即使：[1][2][3][4][5][6][7][8][9][10]
     */
    public java.util.ArrayList<Integer> construct_num_Page() {
        java.util.ArrayList<Integer> current_array = new java.util.ArrayList<Integer>();
        if (this.pageNums < this.sub_pages) {
            // current_array=array();
            for (int i = 0; i < this.pageNums; i++) {
                current_array.add(i, i + 1);
            }
        } else {
            current_array = this.initArray();
            if (this.current_page <= 3) {
                for (int i = 0; i < current_array.size(); i++) {
                    current_array.set(i, i + 1);
                }
            } else if (this.current_page <= this.pageNums
                    && this.current_page > this.pageNums - this.sub_pages + 1) {
                for (int i = 0; i < current_array.size(); i++) {
                    current_array.set(i, (this.pageNums) - (this.sub_pages) + 1
                            + i);
                }
            } else {
                for (int i = 0; i < current_array.size(); i++) {
                    current_array.set(i, this.current_page - 2 + i);
                }
            }
        }
        for (int i = 0; i < current_array.size(); i++)
            System.out.print(current_array.get(i) + " ");
        System.out.println();
        return current_array;
    }
 
    /*
     * 構造普通模式的分頁 共4523條記錄,每頁顯示10條,當前第1/453頁 [首頁] [上頁] [下頁] [尾頁]
     */
    public String subPageCss1() {
        String subPageCss1Str = "";
        subPageCss1Str += "共" + this.nums + "条记录，";
        subPageCss1Str += "每页显示" + this.each_disNums + "条，";
        subPageCss1Str += "当前第" + this.current_page + "/" + this.pageNums
                + "页 ";
        if (this.current_page > 1) {
            String firstPageUrl = this.subPage_link + "1";
            String prewPageUrl = this.subPage_link + (this.current_page - 1);
            subPageCss1Str += "[<a href='" + firstPageUrl + "'>首页</a>] ";
            subPageCss1Str += "[<a href='" + prewPageUrl + "'>上一页</a>] ";
        } else {
            subPageCss1Str += "[首页] ";
            subPageCss1Str += "[上一页] ";
        }
 
        if (this.current_page < this.pageNums) {
            String lastPageUrl = this.subPage_link + this.pageNums;
            String nextPageUrl = this.subPage_link + (this.current_page + 1);
            subPageCss1Str += " [<a href='" + nextPageUrl + "'>下一页</a>] ";
            subPageCss1Str += "[<a href='" + lastPageUrl + "'>尾页</a>] ";
        } else {
            subPageCss1Str += "[下一页] ";
            subPageCss1Str += "[尾页] ";
        }
 
        return subPageCss1Str;
 
    }
 
    /*
     * 構造經典模式的分頁 當前第1/453頁 [首頁] [上頁] 1 2 3 4 5 6 7 8 9 10 [下頁] [尾頁]
     */
    public String subPageCss2() {
        String subPageCss2Str = "";
        //subPageCss2Str += "當前第" + current_page + "/" + pageNums + "頁 ";
 
        if (current_page > 1 && current_page < 10) {
            subPageCss2Str += "<span class='pageTextLink'><a href='javascript:searchPageLink("+ (current_page-1) +",10);'>上一页</a></span>";
            subPageCss2Str += "<span class='pageLink'><a href='javascript:searchPageLink(1,10);'>1</a></span>";
        }else if(current_page >= 10){
        	subPageCss2Str += "<span class='pageTextLink'><a href='javascript:searchPageLink("+ (current_page-1) +",10);'>上一页</a></span>";
            subPageCss2Str += "<span class='pageLink'><a href='javascript:searchPageLink(1,10);'>1</a></span>";
            subPageCss2Str += "<span class=''pageLink'> ... </span>";
        }else {
            subPageCss2Str += "<span class='selectedLink'>1</span>";
        }
 
        java.util.ArrayList<Integer> a = construct_num_Page();
        int s;
        for (int i = 0; i < a.size(); i++) {
            s = a.get(i);
            if (s == current_page) {
                subPageCss2Str += "<span class='selectedLink'>" + s + "</span>";
            } else {
                subPageCss2Str += "<span class='pageLink'><a href='javascript:searchPageLink(" + s +",10);'>" 
                				+ s + "</a></span>";
            }
        }
 
        if (current_page < pageNums) {
            subPageCss2Str += "<span class=''pageLink'> ... </span>";
            subPageCss2Str += "<span class=''pageLink'><a href='javascript:searchPageLink("
            				+ (current_page + 1) + ",10)'>下一頁</a></span>";
            //subPageCss2Str += "[<a href='" + lastPageUrl + "'>尾頁</a>] ";
        } 
        return subPageCss2Str;
    }
    /*
    public static void main(String args[]) {
        SubPages pages = new SubPages(20, 1031, 13, 10,"index.jsp?p=");
        System.out.println(pages.subPageCss2());
    }
    */
}
